<template>
  <div class="login-body">
    <div class="login-center">
      <div class="login-title wow fadeInDown">
        <p>智慧农场数据大屏注册</p>
      </div>
      <div class="login-form">
        <div class="form-title wow fadeInDown">用户注册</div>
        <div class="login-input wow fadeInLeft">
          <input 
            v-model="username" 
            type="text" 
            placeholder="请输入用户名"
            @keyup.enter="handleRegister"
          >
        </div>
        <div class="login-input wow fadeInRight">
          <input 
            v-model="password" 
            type="password" 
            placeholder="请输入密码"
            @keyup.enter="handleRegister"
          >
        </div>
        <div class="info">
          <div class="back wow fadeInLeft">
            <router-link to="/login" class="back-link">返回登录</router-link>
          </div>
          <div v-if="errorMessage" class="error-message wow fadeInRight">
            {{ errorMessage }}
          </div>
        </div>
        <div class="login-button wow fadeInUp" @click="handleRegister" :disabled="isLoading">
          {{ isLoading ? '注册中...' : '立即注册' }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref('')
const password = ref('')
const isLoading = ref(false)
const errorMessage = ref('')

const handleRegister = async () => {
  // 输入验证
  if (!username.value || !password.value) {
    errorMessage.value = '用户名和密码不能为空'
    return
  }

  isLoading.value = true
  errorMessage.value = ''

  try {
    const response = await fetch('http://localhost:8080/api/auth/register', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      },
      body: JSON.stringify({
        username: username.value,
        password: password.value
      })
    })

    const data = await response.json()
    
    if (!response.ok) {
      // 处理后端返回的错误信息
      if (Array.isArray(data)) {
        // 如果是数组形式的错误信息
        throw new Error(data[0])
      } else if (typeof data === 'string') {
        // 如果直接是字符串
        throw new Error(data)
      } else if (data.message || data.error) {
        // 如果是对象形式的错误信息
        throw new Error(data.message || data.error)
      } else {
        throw new Error('注册失败，请稍后重试')
      }
    }

    // 注册成功
    alert('注册成功！')
    router.push('/login')
  } catch (error) {
    console.error('注册错误:', error)
    errorMessage.value = error instanceof Error ? error.message : '注册失败，请稍后重试'
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-body {
  width: 100vw;
  height: 100vh;
  background-image: url('@/assets/background.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-body::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 21, 41, 0.7);
  z-index: 1;
}

.login-center {
  width: 500px;
  padding: 40px;
  position: relative;
  z-index: 2;
}

.login-title {
  text-align: center;
  margin-bottom: 40px;
}

.login-title p {
  color: #00e4ff;
  font-size: 36px;
  font-weight: bold;
  text-shadow: 0 0 15px rgba(0,228,255,0.8);
}

.login-form {
  background: rgba(0, 24, 48, 0.8);
  border-radius: 10px;
  padding: 40px;
  box-shadow: 0 0 25px rgba(0,228,255,0.2);
  font-size: 16px;
}

.form-title {
  color: #00e4ff;
  font-size: 24px;
  text-align: center;
  margin-bottom: 30px;
}

.login-input {
  position: relative;
  margin-bottom: 20px;
}

.login-input input {
  width: 100%;
  height: 45px;
  background: rgba(0,228,255,0.1);
  border: 1px solid #00e4ff;
  border-radius: 5px;
  padding: 0 15px;
  color: #fff;
  font-size: 16px;
}

.login-input input::placeholder {
  color: rgba(0,228,255,0.6);
}

.info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  color: #00e4ff;
}

.back {
  display: flex;
  align-items: center;
  gap: 5px;
}

.back-link {
  color: #00e4ff;
  text-decoration: none;
  cursor: pointer;
}

.back-link:hover {
  text-decoration: underline;
}

.error-message {
  color: #ff4d4f;
  font-size: 14px;
}

.login-button {
  width: 100%;
  height: 45px;
  background: #00e4ff;
  border: none;
  border-radius: 5px;
  color: #001529;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.3s;
}

.login-button:hover {
  background: #00b4cc;
}

.login-button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* 动画效果 */
.wow {
  animation-duration: 1s;
}

.fadeInDown {
  animation-name: fadeInDown;
}

.fadeInLeft {
  animation-name: fadeInLeft;
}

.fadeInRight {
  animation-name: fadeInRight;
}

.fadeInUp {
  animation-name: fadeInUp;
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translate3d(0, -100%, 0);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes fadeInLeft {
  from {
    opacity: 0;
    transform: translate3d(-100%, 0, 0);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes fadeInRight {
  from {
    opacity: 0;
    transform: translate3d(100%, 0, 0);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translate3d(0, 100%, 0);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
</style> 